/*** 
 * @Author: pengcheng
 * @Date: 2021-06-24 10:27:50
 * @LastEditTime: 2021-08-24 14:10:39
 * @LastEditors: pengcheng
 * @Description: 
 * @FilePath: /kylin-daq/src/DataTransmit.cpp
 */
#include "TransmitModule.h"

QScopedPointer<TransmitModule> TransmitModule::ms_instance;
TransmitModule::TransmitModule(QObject *parent)
    : BaseModule(s_moduleName,parent)
    , m_mannager(new QNetworkAccessManager(this))
    , uploadTimer(new QTimer(this))
    , reuploadTimer(new QTimer(this))
    , uploadStateInfoTimer(new QTimer(this))
{
    connect(this, &TransmitModule::uploadIntervalChanged, this, &TransmitModule::setUploadTimerPollingInterval);
    connect(this, &TransmitModule::reuploadIntervalChanged, this, &TransmitModule::setReuploadTimerPollingInterval);
    connect(this, &TransmitModule::uploadStateInfoIntervalChanged, this, &TransmitModule::setUploadStateInfoTimerPollingInterval);
    connect(uploadTimer, &QTimer::timeout, this , &TransmitModule::sendUploadInfoNum);
    connect(reuploadTimer, &QTimer::timeout, this , &TransmitModule::sendReuploadInfoNum);
    connect(uploadStateInfoTimer, &QTimer::timeout, this , &TransmitModule::sendStateInfoNum);
}

TransmitModule::~TransmitModule()
{
}

TransmitModule::SendInfoStatus TransmitModule::sendInfo(const QList<QString> &info, const QString &urlValue, const QString &requestHead, const int32_t timeCountMsec)
{ 
    QString responseBody;
    qDebug()<< "TransmitModule::sendInfo: urlValue="<<urlValue;
    QUrl url(urlValue);
    SendInfoStatus requestState=SendInfoStatus::OK;
    QString requestErrorRelyStr;
    int32_t requestErrorCode = 0;
    QJsonDocument requestHeadDoc = QJsonDocument::fromJson(requestHead.toUtf8());
    QJsonObject requestHeadObj =  requestHeadDoc.object();
    QByteArray requestData;
    //	检测url
    requestState = checkUrl(url);
    if(requestState == SendInfoStatus::OK)
    {
        // 检测待发送数据
        requestState = mergeInfo(info,requestData);
        if(requestState == SendInfoStatus::OK)
        {
            if(m_mannager->networkAccessible() == QNetworkAccessManager::NotAccessible)
            {
                m_mannager->setNetworkAccessible(QNetworkAccessManager::Accessible);
            }
            QNetworkRequest request;
            // 设置请求相关参数
            requestState = setRequest(request,url,requestHeadObj);
            if(requestState == SendInfoStatus::OK)
            {
                requestState = sendRequest(request,requestData,timeCountMsec,requestErrorCode,requestErrorRelyStr,responseBody);
            }
        }
    }

    //  构造发送数据
    //  检查网络
    //  发送数据
    printSendInfoLog(info,requestState,requestErrorCode,requestErrorRelyStr);
    return requestState;
}

TransmitModule::SendInfoStatus TransmitModule::getInfo(const QString &urlValue, const QString &requestHead, const QString &requestBody, const int32_t timeCountMsec, QString &reponseBody)
{
    qDebug()<< "TransmitModule::getInfo: urlValue="<<urlValue;
    QList<QString> info;
    QUrl url(urlValue);
    SendInfoStatus requestState=SendInfoStatus::OK;
    QString requestErrorRelyStr;
    int32_t requestErrorCode = 0;
    QByteArray requestData;
    QJsonDocument requestHeadDoc = QJsonDocument::fromJson(requestHead.toUtf8());
    QJsonObject requestHeadObj;
    if (requestHeadDoc.isObject())
    {
        requestHeadObj = requestHeadDoc.object();
    }
    //	检测url
    requestState = checkUrl(url);
    if(requestState == SendInfoStatus::OK)
    {
        // 检测待发送数据
        if(requestState == SendInfoStatus::OK)
        {
            if(m_mannager->networkAccessible() == QNetworkAccessManager::NotAccessible)
            {
                m_mannager->setNetworkAccessible(QNetworkAccessManager::Accessible);
            }
            QNetworkRequest request;
            // 设置请求相关参数
            requestState = setRequest(request,url,requestHeadObj);
            if(requestState == SendInfoStatus::OK)
            {
                requestState = sendRequest(request,requestBody.toUtf8(),timeCountMsec,requestErrorCode,requestErrorRelyStr,reponseBody);
            }
        }
    }
    printSendInfoLog(info,requestState,requestErrorCode,requestErrorRelyStr);
    return requestState;

}

TransmitModule::SendInfoStatus TransmitModule::checkUrl(const QUrl &urlValue)
{
    //	检测url
    if(!urlValue.isValid())
    {
        return SendInfoStatus::FormatException;
    }
    if(urlValue.scheme() != "http"&&urlValue.scheme() != "https")
    {
        return SendInfoStatus::FormatException;
    }
    return SendInfoStatus::OK;

}

TransmitModule::SendInfoStatus TransmitModule::mergeInfo(const QList<QString> &info, QByteArray &requestData)
{
    QJsonArray mergeJsonObject;
    QJsonObject sendedJsonObject;
    foreach(const QString &tempInfo, info)
    {
        QJsonDocument tempJsonDoc = QJsonDocument::fromJson(tempInfo.toUtf8());
        if(!tempJsonDoc.isObject())
        {
            continue;
        }
        QJsonObject tempJsonObj =  tempJsonDoc.object();
        if(!tempJsonObj.contains(KylinDaqCommon::gs_sourceKey)
            || !tempJsonObj.contains(KylinDaqCommon::gs_informationKey)
            || !tempJsonObj[KylinDaqCommon::gs_sourceKey].isObject()
            || !tempJsonObj[KylinDaqCommon::gs_informationKey].isObject())
        {
            continue;
        }
        QJsonObject sourceJsonObj(tempJsonObj[KylinDaqCommon::gs_sourceKey].toObject());
        QJsonObject informationJsonObj(tempJsonObj[KylinDaqCommon::gs_informationKey].toObject());
        // 去除information中不符合条件的数据
        if(informationJsonObj.contains(KylinDaqCommon::gs_packageNameKey)
                || informationJsonObj.contains(KylinDaqCommon::gs_messageTypeKey)
                || informationJsonObj.contains(KylinDaqCommon::gs_tidKey)
                || informationJsonObj.contains(KylinDaqCommon::gs_parentTidKey))
        {
           continue;
        }
        QJsonObject  waitAddJsonObj(informationJsonObj);
        if(!sourceJsonObj.contains(KylinDaqCommon::gs_packageNameKey)
                || !sourceJsonObj.contains(KylinDaqCommon::gs_messageTypeKey)
                || !sourceJsonObj.contains(KylinDaqCommon::gs_tidKey))
        {
            continue;
        }
        waitAddJsonObj.insert(KylinDaqCommon::gs_packageNameKey,sourceJsonObj[KylinDaqCommon::gs_packageNameKey].toString());
        waitAddJsonObj.insert(KylinDaqCommon::gs_messageTypeKey,sourceJsonObj[KylinDaqCommon::gs_messageTypeKey].toString());
        waitAddJsonObj.insert(KylinDaqCommon::gs_tidKey,sourceJsonObj[KylinDaqCommon::gs_tidKey].toString());
        waitAddJsonObj.insert(KylinDaqCommon::gs_sourceVersionKey,KylinDaqCommon::gs_sourceVersionValue);
        if(sourceJsonObj.contains(KylinDaqCommon::gs_parentTidKey))
        {
            waitAddJsonObj.insert(KylinDaqCommon::gs_parentTidKey,sourceJsonObj[KylinDaqCommon::gs_parentTidKey].toString());
        }
        if(waitAddJsonObj.contains(s_uploadTimeKey)) {
            waitAddJsonObj[s_uploadIntervalKey]=QDateTime::currentDateTime().toTimeZone(QTimeZone(KylinDaqCommon::gs_timezone)).toString(KylinDaqCommon::gs_timeFormat);
        }
        else
        {
            waitAddJsonObj.insert(s_uploadTimeKey,QDateTime::currentDateTime().toTimeZone(QTimeZone(KylinDaqCommon::gs_timezone)).toString(KylinDaqCommon::gs_timeFormat));
        }
        if(sourceJsonObj.contains(KylinDaqCommon::gs_stateOperatorKey)) 
        {
            waitAddJsonObj.insert(KylinDaqCommon::gs_stateOperatorKey,sourceJsonObj[KylinDaqCommon::gs_stateOperatorKey]);
        }
        mergeJsonObject.append(waitAddJsonObj);
    }
    if(mergeJsonObject.isEmpty())
    {
        return SendInfoStatus::FormatException;
    }
    sendedJsonObject.insert(s_messageKey,mergeJsonObject);
    requestData = QJsonDocument(sendedJsonObject).toJson(QJsonDocument::Compact);
    return SendInfoStatus::OK;

}

TransmitModule::SendInfoStatus TransmitModule::setRequest(QNetworkRequest &request, const QUrl &url, const QJsonObject &requestHeadObj)
{
    if(url.scheme() == "https")
    {
        QSslConfiguration config;
        config.setProtocol(QSsl::TlsV1SslV3);
        config.setPeerVerifyMode(QSslSocket::PeerVerifyMode::VerifyNone);
        request.setSslConfiguration(config);
    }
    //	设置请求
    for(auto iter = requestHeadObj.begin();iter != requestHeadObj.end();iter++)
    {
        if(iter.value().isString())
        {
            qDebug()<<iter.value().isString();
            request.setRawHeader(iter.key().toUtf8(),iter.value().toString().toUtf8());
        }
    }
    request.setRawHeader(s_httpUploadTimeStamp,QDateTime::currentDateTime().toTimeZone(QTimeZone(KylinDaqCommon::gs_timezone)).toString(KylinDaqCommon::gs_timeFormat).toUtf8());
    request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(s_requestTyepValue));
    request.setUrl(url);
    return SendInfoStatus::OK;
}

TransmitModule::SendInfoStatus TransmitModule::sendRequest(QNetworkRequest &request, const QByteArray &requestData, const int32_t timeCountMsec, int32_t &requestErrorCode, QString &requestErrorRelyStr,QString &responseBody)
{
    QTimer timer;
    QEventLoop eventLoop;
    SendInfoStatus requestState = SendInfoStatus::OK;
    QNetworkReply *tempReply = m_mannager->post(request, requestData);
    connect(tempReply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    connect(&timer, &QTimer::timeout, &eventLoop, &QEventLoop::quit);
    timer.setSingleShot(true);
    timer.start(timeCountMsec);
    eventLoop.exec();
    if (!timer.isActive()) {
        qDebug() << "post响应超时";
        tempReply->abort();
    }
    if (tempReply->error() == QNetworkReply::NoError) {
        QVariant stateCode = tempReply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
        if (stateCode.isValid()) {
            if (stateCode.toInt() == s_httpReplyValidCode) {
                requestState = SendInfoStatus::OK;
                QJsonDocument replyDoc = QJsonDocument::fromJson(tempReply->readAll());
                if(replyDoc.isObject())
                {
                    QJsonObject replyObj = replyDoc.object();
                    if (replyObj.contains(s_replyErrorCodeKey))
                    {
                        if (replyObj[s_replyErrorCodeKey].toInt() != s_replyValidCode)
                        {
                            requestState = SendInfoStatus::ServerReplyException;
                            requestErrorCode = replyObj[s_replyErrorCodeKey].toInt();
                            if (replyObj.contains(s_replyErrorMsgKey))
                            {
                                requestErrorRelyStr = replyObj[s_replyErrorMsgKey].toString();
                            }
                        }
                        if (replyObj.contains(s_replyResponseBodyKey) && replyObj[s_replyResponseBodyKey].isObject())
                        {
                            responseBody = QString(QJsonDocument(replyObj[s_replyResponseBodyKey].toObject()).toJson());
                        }
                    }
                }
            } else {
                requestState = SendInfoStatus::ServerException;
                requestErrorCode = stateCode.toInt();
            }
        } else {
            requestState = SendInfoStatus::ServerException;
        }
    } else {
        requestState = SendInfoStatus::NetworkAnomaly;
        requestErrorCode = tempReply->error();
    }
    tempReply->deleteLater();
    return requestState;
}

void TransmitModule::printSendInfoLog(const QList<QString> &info, const TransmitModule::SendInfoStatus &requestState, int32_t requestErrorCode, const QString &requestErrorRelyStr)
{
    switch (requestState)
    {
    case SendInfoStatus::OK:
        qInfo().noquote() << QString("TransmitModule::sendInfo: requestInfoNum = %1,requestState=%2").arg(info.size()).arg(QString::number(requestState));
        break;
    case SendInfoStatus::FormatException:
        qInfo().noquote() << QString("TransmitModule::sendInfo: requestInfoNum = %1,requestState=%2").arg(info.size()).arg(QString::number(requestState));
        break;
    case SendInfoStatus::NetworkAnomaly:
        qInfo().noquote() << QString("TransmitModule::sendInfo: requestInfoNum = %1,requestState=%2, ErrorCode=%3").arg(info.size()).arg(QString::number(requestState),QString::number(requestErrorCode));
        break;
    case SendInfoStatus::ServerException:
        qInfo().noquote() << QString("TransmitModule::sendInfo: requestInfoNum = %1,requestState=%2, ErrorCode=%3").arg(info.size()).arg(QString::number(requestState),QString::number(requestErrorCode) );
        break;
    case SendInfoStatus::ServerReplyException:
        qInfo().noquote() << QString("TransmitModule::sendInfo: requestInfoNum = %1,requestState=%2, ErrorCode=%3, RelyStr=%4").arg(info.size()).arg(QString::number(requestState),QString::number(requestErrorCode) ,requestErrorRelyStr);
        break;
    case SendInfoStatus::UrlException:
        qInfo().noquote() << QString("TransmitModule::sendInfo: requestInfoNum = %1,requestState=%2").arg(info.size()).arg(QString::number(requestState));
        break;
    default:
        break;
    }
}

void TransmitModule::sendUploadInfoNum()
{
    qInfo()<<"upload state = "<<uploadState();
    if(uploadState() == KylinDaqCommon::UploadEnable)
    {
       emit uploadInfoSignal(m_numOfUploadedLines);
    }
}

void TransmitModule::sendReuploadInfoNum()
{
    if(uploadState() == KylinDaqCommon::UploadEnable)
    {
       emit reuploadInfoSignal(m_numOfReuploadedLines);
    }
}

void TransmitModule::sendStateInfoNum()
{
    if(uploadState() == KylinDaqCommon::UploadEnable)
    {
       emit uploadStateInfoSignal(m_numOfUploadedStateInfoLines);
    }
}

void TransmitModule::uploadInfo(const QList<qlonglong> &id,const QList<QString> &info,const QList<QString> & status, const QString &requestHead)
{
    qInfo()<<"TransmitModule::uploadInfo start";
    if(info.size()>0 )
//    if(info.size()>0 && networkIsConnected())
    {
        SendInfoStatus retSendInfoStatus;
        foreach(QString realm, m_uploadRealm)
        {
            retSendInfoStatus = sendInfo(info,realm + s_uploadUrl,requestHead,KylinDaqCommon::secToMsec(uploadRequestMaxTime()));
            qInfo()<< "TransmitModule::uploadInfo";
            if(retSendInfoStatus == SendInfoStatus::OK)
            {
                break;
            }
        }
        emit retUploadState(id,info,status, retSendInfoStatus);
    }
}

void TransmitModule::reuploadInfo(const QList<qlonglong> &id,const QList<QString> &info,const QList<QString> & status, const QString &requestHead)
{
    qInfo()<<"TransmitModule::reuploadInfo start";
    if(info.size()>0 )
//    if(info.size()>0 && networkIsConnected())
    {
        SendInfoStatus retSendInfoStatus;
        foreach(QString realm, m_uploadRealm)
        {
            retSendInfoStatus = sendInfo(info,realm + s_reuploadUrl,requestHead,KylinDaqCommon::secToMsec(uploadRequestMaxTime()));
            qInfo()<< "TransmitModule::reuploadInfo";
            if(retSendInfoStatus == SendInfoStatus::OK)
            {
                break;
            }
        }
        emit retReuploadState(id,info,status, retSendInfoStatus);
    }
}

void TransmitModule::uploadStateInfo(const QList<QString> &packageInfoList,const QList<QString> &uploadedMessageList, const QList<QString> &tidsList, const QList<QString> &createTimeStampList,const QList<KylinDaqCommon::UploadStateEnum> & uploadStateList, const QString &requestHead)
{
    qInfo()<<"TransmitModule::uploadStateInfo start";
    if(packageInfoList.size()>0)
    {
        QList<QString> infoList;
        for(int32_t index = 0;index < packageInfoList.size();index++)
        {
            QString info = createStateInfo(packageInfoList[index],uploadedMessageList[index],tidsList[index],createTimeStampList[index],uploadStateList[index]);
            if(info != "")
            {
                infoList.append(info);
            }
        }
        SendInfoStatus retSendInfoStatus;
        foreach(QString realm, m_uploadRealm)
        {

            retSendInfoStatus = sendInfo(infoList,realm + s_stateInfoUploadUrl,requestHead,KylinDaqCommon::secToMsec(uploadRequestMaxTime()));
            qInfo()<< "TransmitModule::uploadStateInfo";
            if(retSendInfoStatus == SendInfoStatus::OK)
            {
                break;
            }
        }
        emit retUploadStateInfoState(packageInfoList, retSendInfoStatus);
    }
}

void TransmitModule::setUploadTimerPollingInterval(qlonglong sec)
{
    uploadTimer->stop();
    uploadTimer->setInterval(KylinDaqCommon::secToMsec(sec));
    uploadTimer->start();
}

void TransmitModule::setReuploadTimerPollingInterval(qlonglong sec)
{
    reuploadTimer->stop();
    reuploadTimer->setInterval(KylinDaqCommon::secToMsec(sec));
    reuploadTimer->start();
}


void TransmitModule::setUploadStateInfoTimerPollingInterval(qlonglong sec)
{
    uploadStateInfoTimer->stop();
    uploadStateInfoTimer->setInterval(KylinDaqCommon::secToMsec(sec));
    uploadStateInfoTimer->start();
}

void TransmitModule::printConf()
{
    qDebug()<<"uploadInterval:"<<m_uploadInterval;
    qDebug()<<"reuploadInterval:"<<m_reuploadInterval;
    qDebug()<<"stateInfoUploadInterval:"<<m_uploadStateInfoInterval;
    qDebug()<<"numOfUploadedLines:"<<m_numOfUploadedLines;
    qDebug()<<"numOfReuploadedLines:"<<m_numOfReuploadedLines;
    qDebug()<<"numOfStateInfoUploadedLines:"<<m_numOfUploadedStateInfoLines;
    qDebug()<<"uploadState:"<<m_uploadState;
    qDebug()<<"uploadRealm:"<<m_uploadRealm;
    qDebug()<<"uploadRequestMaxTime:"<<m_uploadRequestMaxTime;
}

void TransmitModule::getConfig()
{
    qInfo()<<"TransmitModule::getConfig start";
    QString requestHead;
    QString requestBody;
    QString responseBody;
    SendInfoStatus retSendInfoStatus;
    foreach(QString realm, m_uploadRealm)
    {
        retSendInfoStatus = getInfo(realm + s_configUrl,requestHead,requestBody,KylinDaqCommon::secToMsec(uploadRequestMaxTime()),responseBody);
        qInfo()<< "TransmitModule::get config Info";
        if(retSendInfoStatus == SendInfoStatus::OK)
        {
            emit sendConfig(responseBody);
            break;
        }
    }
}

void TransmitModule::updateProperty(const QJsonObject &conf, const QJsonObject &defaultConf)
{
    qlonglong value;
    qlonglong uploadIntervalSum;
    QList<QString> tempStrList;
    KylinDaqCommon::setValidConfInt(conf, defaultConf, moduleName(), s_numOfUploadedLinesKey, value);
    setNumOfUploadedLines(value);
    KylinDaqCommon::setValidConfInt(conf, defaultConf, moduleName(), s_numOfReuploadedLinesKey, value);
    setNumOfReuploadedLines(value);
    KylinDaqCommon::setValidConfInt(conf, defaultConf, moduleName(), s_numOfStateInfoUploadedLinesKey, value);
    setNumOfUploadedStateInfoLines(value);
    KylinDaqCommon::setValidConfInt(conf, defaultConf, moduleName(), s_uploadStateKey, value);
    setUploadState(KylinDaqCommon::UploadState(value));
    KylinDaqCommon::setValidConfStrArray(conf, defaultConf, moduleName(), s_uploadRealmKey, tempStrList);
    setUploadRealm(tempStrList);
    KylinDaqCommon::setValidConfInt(conf, defaultConf, moduleName(), s_uploadRequestMaxTimeKey, value);
    setUploadRequestMaxTime(value);
    uploadIntervalSum = uploadRealm().size()*uploadRequestMaxTime()*2 + s_uploadIntervalThreshold; 
    KylinDaqCommon::setValidConfIntCmpMax(conf, defaultConf, moduleName(), s_uploadIntervalKey, uploadIntervalSum, value);
    setUploadInterval(value);
    KylinDaqCommon::setValidConfIntCmpMax(conf, defaultConf, moduleName(), s_reuploadIntervalKey, uploadIntervalSum, value);
    setReuploadInterval(value);
    KylinDaqCommon::setValidConfIntCmpMax(conf, defaultConf, moduleName(), s_stateInfoUploadIntervalKey, uploadIntervalSum, value);
    setUploadStateInfoInterval(value);
    printConf();
}

TransmitModule *TransmitModule::instance()
{
    if (ms_instance.isNull()) {
        ms_instance.reset(new TransmitModule());
    }
    return ms_instance.data();

}

/*判断网络是否连接*/
bool TransmitModule::networkIsConnected()
{
    QDBusInterface *networkService =  new QDBusInterface("org.freedesktop.NetworkManager",
                                                         "/org/freedesktop/NetworkManager",
                                                         "org.freedesktop.NetworkManager",
                                                         QDBusConnection::systemBus());
    if(!networkService->isValid())
    {
        qWarning() << "网络Service Interface: " << qPrintable(QDBusConnection::systemBus().lastError().message());
        return false;
    }
    QDBusMessage message = networkService->call(QDBus::Block, "state");
    if (message.type() == QDBusMessage::ReplyMessage)
    {
        //从返回参数获取返回值
        uint32_t value = message.arguments().takeFirst().toUInt();
        if (value == 70)
        {
            return true;
        }
        else
        {
            qWarning() << "网络状态接口返回值：" << value;
            return false;
        }
    }
    qWarning() << "获取网络状态接口返回值失败";
    return false;
}


QString TransmitModule::createStateInfo(const QString &packageInfo, const QString &uploadedMessage, const QString &tids, const QString &createTimeStamp,const KylinDaqCommon::UploadStateEnum &uploadState)
{
    QString info;
    QJsonObject infoJsonObj;
    QJsonObject sourceJsonObj;
    QJsonObject informationJsonObj;
    QJsonDocument packageInfoJsonDoc = QJsonDocument::fromJson(packageInfo.toUtf8());
    QJsonDocument uploadedMessageJsonDoc = QJsonDocument::fromJson(uploadedMessage.toUtf8());
    QJsonDocument tidsJsonDoc = QJsonDocument::fromJson(tids.toUtf8());
    QJsonDocument createTimeStampJsonDoc = QJsonDocument::fromJson(createTimeStamp.toUtf8());
    
    if (!packageInfoJsonDoc.isObject()
        ||!uploadedMessageJsonDoc.isObject()
        ||!tidsJsonDoc.isObject()
        ||!createTimeStampJsonDoc.isObject()
    )
    {
        return info;
    }
    QJsonObject packageInfoJsonObj = packageInfoJsonDoc.object();
    QJsonObject uploadedMessageJsonObj = uploadedMessageJsonDoc.object();
    QJsonObject tidsJsonObj = tidsJsonDoc.object();
    QJsonObject createTimeStampJsonObj = createTimeStampJsonDoc.object();

    sourceJsonObj = packageInfoJsonObj;
    informationJsonObj = uploadedMessageJsonObj;

    if (!packageInfoJsonObj.contains(KylinDaqCommon::gs_packageNameKey)
        ||!packageInfoJsonObj.contains(KylinDaqCommon::gs_messageTypeKey)
        ||!tidsJsonObj.contains(KylinDaqCommon::gs_tidKey)
        ||!createTimeStampJsonObj.contains(KylinDaqCommon::gs_createTimeStampKey))
    {
        return info;
    }
    if (uploadState != KylinDaqCommon::UploadStateEnum::UploadStateMessageCreated
        && uploadState != KylinDaqCommon::UploadStateEnum::UploadStateMessageUpdated)
    {
        return info;
    }

    sourceJsonObj[KylinDaqCommon::gs_tidKey]=tidsJsonObj[KylinDaqCommon::gs_tidKey];
    sourceJsonObj[KylinDaqCommon::gs_parentTidKey]=tidsJsonObj[KylinDaqCommon::gs_parentTidKey];
    informationJsonObj[KylinDaqCommon::gs_createTimeStampKey] = createTimeStampJsonObj[KylinDaqCommon::gs_createTimeStampKey];
    if (uploadState == KylinDaqCommon::UploadStateEnum::UploadStateMessageCreated)
    {
        sourceJsonObj[KylinDaqCommon::gs_stateOperatorKey] = KylinDaqCommon::gs_stateOperatorKeyAddValue;
    }
    else if (uploadState == KylinDaqCommon::UploadStateEnum::UploadStateMessageUpdated)
    {
        sourceJsonObj[KylinDaqCommon::gs_stateOperatorKey] = KylinDaqCommon::gs_stateOperatorKeyUpdateValue;
    }
    infoJsonObj[KylinDaqCommon::gs_sourceKey] = sourceJsonObj;
    infoJsonObj[KylinDaqCommon::gs_informationKey] = informationJsonObj;
    info = QString(QJsonDocument(infoJsonObj).toJson(QJsonDocument::Compact));
    return info; 
}
